package com.chb.javabasic.jdbc;

import com.chb.javabasic.jdbc.DAO.UserDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertFalse;

@Slf4j
@Component
public class UserTrying {

    @Autowired
    private UserDao userDao;

    public void select(String Username) {
        List<User> users = userDao.findByUsername(Username);
        if (users.isEmpty()) {
            throw new AssertionError("用户不存在!");
        }
        System.out.println("用户信息: " + users);
    }

    public void insert() {
        User user = new User();
        user.setUsername("luxi");
        user.setPassword("123456");
        user.setEmail("3116313350@qq.com");
        user.setCreatedAt(Timestamp.valueOf(LocalDateTime.now()));
        user.setUpdatedAt(Timestamp.valueOf(LocalDateTime.now()));
        userDao.insertUser(user);
        log.info("插入成功，插入用户信息");
        select(user.getUsername());
    }

    public void delete(Long id) {
        userDao.DeleteById(id);
        log.info("删除成功!");
    }

    public void update(Long id) {
        User user = new User();
        user.setId(id);
        user.setUsername("xiaoming");
        user.setPassword("123456");
        user.setEmail("3116313350@qq.com");
        user.setUpdatedAt(Timestamp.valueOf(LocalDateTime.now()));
        userDao.UpdateUser(user);
        log.info("更新成功!");
    }

}
